package portal.conf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.QueueEntity;

@Deprecated
public abstract class BaseConfOperation {
	protected static long lastModified=-1;
	public  List<Object> conf=new ArrayList<Object>();
	public  void addConf(String[] queue) {
		FileWriter fw = null;
		try {
			//如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f=new File("//app//unischeduler//queues.conf");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		for(String ele:queue){
			pw.println(ele);
		}
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void addConf(Object qe){
		if(qe==null)return;
		addConf(qe.toString());
	}
	public  void addConf(String string){
		if(StringUtils.isEmpty(string))
			return;
		addConf(string.split("\n"));
	}
	/*public static void main(String arg[]){
		QueueEntity qe=new QueueEntity();
		qe.setQUEUE_NAME("test0001");
		addConf(qe);
		System.out.println("end!!!");
	}*/
	/**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
	public synchronized <T> void readFileByLines(String fileName) {
	    File file = new File(fileName);
	    if(file.lastModified()<= lastModified)
	    	return;
	    lastModified=file.lastModified();
	    		
	    BufferedReader reader = null;
	    try {
			//System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 0;
			BaseConf qe=null;
			conf.clear();
				// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				line++;
				tempString=MyStringUtils.trimAll(tempString);
				if(MyStringUtils.isEmpty(tempString)||tempString.startsWith("#"))continue;
				qe=(BaseConf) analysiConf(tempString,qe);
				if(isQueueStart(tempString)){
					if(qe!=null)
						//qe=T.getClass().newInstance();
					qe.setLineStart(line);
				}
				if(isQueueEnd(tempString)){
					qe.setLineEnd(line);
					conf.add(qe);
					//qe=null;
				}
	        }
	      reader.close();
	    } catch (IOException e) {
	      e.printStackTrace();
	    } finally {
	      if (reader != null){
            try {
                reader.close();
            } catch (IOException e1) {
	            }
	        }
	    }
	}
	public Object analysiConf(String line,Object queue){
		
		try {
			if(queue==null)
			queue =(Object) queue.getClass().newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(isQueueStart(line)||isQueueEnd(line))
			return queue;
		findEle(line);
		
		return queue;
	}
	public static boolean isQueueStart(String line){
		if(line.contains("Begin Queue"))
			return true;
		return false;
	}
	public static boolean isQueueEnd(String line){
		if(line.contains("End Queue"))
			return true;
		return false;
	}
	public abstract boolean findEle(String line);
	/*public static void main(String[] args){
		readFileByLines("//app//unischeduler//queues.conf");
		System.out.println("lenth:"+conf.size());
		for(QueueEntity qu:conf){
			System.out.println(qu.getLineStart());
			System.out.println(qu.toString());
			System.out.println(qu.getLineEnd());
		}
	}*/
	/*public static<Object>  List<Object> getConfList(String fileName){
		readFileByLines(fileName);
		return conf;	
	}*/
	
}
